#include "sResource.h"
#include "sConfig.h"
#include "sResourceContent.h"

namespace sgui
{
namespace res
{
    // ---------------------------------------------- sResourceItem -----------------------------------------------

    sResourceItem::sResourceItem(sGUI&gui, const std::wstring& name) :
        sNamedObject(gui, name)
    {

    }

    // ---------------------------------------------- sResource -----------------------------------------------

    void sResource::clear()
    {
        for (u32 i = 0, t = m_contents.size(); i < t; ++i)
        {
            delete m_contents[i];
        }
        m_contents.clear();
    }

	sResourceContent* sResource::addContent()
	{
		sResourceContent* cnt = new sResourceContent(getGui());
		m_contents.push_back(cnt);
		return cnt;
	}

    void sResource::load(const db::Record &file)
    {
        sNamedObject::load(file);
        const db::Record &contents = file[L"contents"];
        if (contents.getClass() == db::RC_ARRAY)
        {
            for (u32 i = 0, t = contents.getSize(); i < t; ++i)
            {
                sResourceContent* cnt = new sResourceContent(getGui());
                cnt->load(contents[i]);
                m_contents.push_back(cnt);
            }
        }
    }

    void sResource::save(db::Record &file) const
    {
        sNamedObject::save(file);
		file.Set(getClass());

        db::Record &contents = file[L"contents"];
        contents.setSize(m_contents.size());

        for (u32 i = 0, t = m_contents.size(); i < t; ++i)
        {
            m_contents[i]->save(contents[i]);
        }
    }

    std::vector<sResourceContent*>& sResource::getContents() 
    {
        return m_contents;
    }

    const std::vector<sResourceContent*>& sResource::getContents() const
    {
        return m_contents;
    }

#ifdef _EDITOR
	void sResource::exportRTTI(rtti::sInfoList& list)
	{
		sNamedObject::exportRTTI(list);
	}
#endif
	
	sResource::sResource(sGUI&gui, const std::wstring& name) :
		sNamedObject(gui, name)
    {

    }

};
};